package cn.com.infohold.smartcity.update;

import android.content.Context;
import android.text.TextUtils;

import cn.com.infohold.smartcity.api.GsonUtils;
import cn.com.infohold.smartcity.api.bean.Version;
import library.utils.XmlEditor;

/**
 * Created by Mr.Fan on 2017/1/29.
 */

public class UpdateXml extends XmlEditor {
    private static final String VERSION_INFO = "VERSION_INFO";

    private static final String APK_DOWNLOAD_ID = "APK_DOWNLOAD_ID";
    private static final String APK__DOWNLOAD_PATH = "APK__DOWNLOAD_PATH";


    public static UpdateXml obtain(Context contex) {
        return new UpdateXml();
    }

    public void setNewVersion(Version version) {
        putString(VERSION_INFO, GsonUtils.toJson(version));
        commit();
    }

    public Version getNewVersion() {
        String json = getString(VERSION_INFO, null);
        if (TextUtils.isEmpty(json))
            return null;
        return GsonUtils.fromJson(json, Version.class);
    }


    public void setApkDownloadId(Long id) {
        if (id != null)
            putLong(APK_DOWNLOAD_ID, id);
        else
            remove(APK_DOWNLOAD_ID);
        commit();
    }

    public long getApkDownloadId() {
        return getLong(APK_DOWNLOAD_ID, -1L);
    }

    public void setApkPath(String path) {
        if (!TextUtils.isEmpty(path))
            putString(APK__DOWNLOAD_PATH, path);
        else
            remove(APK__DOWNLOAD_PATH);
        commit();
    }

    public String getApkPath() {
        return getString(APK__DOWNLOAD_PATH, null);
    }

}
